package com.wedo.mall.adapter;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.e.library.activity.EActivity;
import com.e.library.adapter.Adapter;
import com.e.library.http.EApi;
import com.e.library.http.EClient;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.listener.EListener;
import com.wedo.mall.R;
import com.wedo.mall.activity.payment.PayActivity;
import com.wedo.mall.activity.user.OrderDetailActivity;
import com.wedo.mall.dialog.SimpleDialog;
import com.wedo.mall.entity.Order;
import com.wedo.mall.widget.GoodsListView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;

/**
 * Created by Lawrence on 2017/8/7.
 */

public class OrderAdapter extends Adapter<Order> {

    Context context;
    public static final String EXTRA_ORDER_SN = "order_sn";

    public OnOrderFragmentListener oofListener;

    public OrderAdapter(Context context, List<Order> orders) {
        super(context, orders);
        this.context = context;
    }

    @Override
    protected int layout() {
        return R.layout.item_order_unpaid;
    }

    @Override
    protected void bind(EHolder holder, Order order, int position) {
        TextView tv_status = holder.find(R.id.tv_status);
        TextView tv_order_sn = holder.find(R.id.tv_order_sn);
        TextView tv_total = holder.find(R.id.tv_total);
        TextView tv_goodsnumber = holder.find(R.id.tv_goodsnumber);
        GoodsListView glv_goods = holder.find(R.id.glv_goods);
        RelativeLayout rl_pay = holder.find(R.id.rl_pay);
        RelativeLayout rl_receive = holder.find(R.id.rl_receive);

        Button btn_pay = holder.find(R.id.btn_pay);
        Button btn_cancel = holder.find(R.id.btn_cancel);
        Button btn_receive = holder.find(R.id.btn_receive);

        //（0.待支付，1.待发货，2.待收货，3.待评价，4.已完成，5.作废，6.退换货）
        String status = "";
        if(order.status.equals("0")){
            status = context.getString(R.string.user_unpaid);
            rl_pay.setVisibility(View.VISIBLE);
            rl_receive.setVisibility(View.GONE);

            /**立即支付*/
            toPay(btn_pay,order);
            /**取消点单*/
            toCancel(btn_cancel,order.order_sn);
            btn_receive.setOnClickListener(null);

        }else if(order.status.equals("1")){
            status = context.getString(R.string.user_unprocessed);
            rl_pay.setVisibility(View.GONE);
            rl_receive.setVisibility(View.GONE);

            btn_pay.setOnClickListener(null);
            /**取消点单*/
            toCancel(btn_cancel,order.order_sn);
            btn_receive.setOnClickListener(null);

        }else if(order.status.equals("2")){
            status = context.getString(R.string.user_notreceived);
            rl_pay.setVisibility(View.GONE);
            rl_receive.setVisibility(View.VISIBLE);
            btn_pay.setOnClickListener(null);
            btn_cancel.setOnClickListener(null);
            toReceived(btn_receive,order.order_sn);

        }else if(order.status.equals("3")){
            status = context.getString(R.string.user_pendingevaluation);
            rl_pay.setVisibility(View.GONE);
            rl_receive.setVisibility(View.GONE);
            btn_pay.setOnClickListener(null);
            btn_cancel.setOnClickListener(null);
            btn_receive.setOnClickListener(null);
        }else if(order.status.equals("4")){
            status = context.getString(R.string.user_finished);
            rl_pay.setVisibility(View.GONE);
            rl_receive.setVisibility(View.GONE);
            btn_pay.setOnClickListener(null);
            btn_cancel.setOnClickListener(null);
            btn_receive.setOnClickListener(null);
        }else if(order.status.equals("5")){
            status = context.getString(R.string.user_nullify);
            rl_pay.setVisibility(View.GONE);
            rl_receive.setVisibility(View.GONE);
            btn_pay.setOnClickListener(null);
            btn_cancel.setOnClickListener(null);
            btn_receive.setOnClickListener(null);
        }else if(order.status.equals("6")){
            status = context.getString(R.string.user_returned);
            rl_pay.setVisibility(View.GONE);
            rl_receive.setVisibility(View.GONE);
            btn_pay.setOnClickListener(null);
            btn_cancel.setOnClickListener(null);
            btn_receive.setOnClickListener(null);
        }
        tv_status.setText(status);
        tv_order_sn.setText(order.order_sn);
        tv_total.setText(order.total);
        tv_goodsnumber.setText(order.goods_number+"");
        glv_goods.record(order.goodsList);
    }

    private void toPay(Button btn_pay, final Order order){
        btn_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PayActivity.toPay(mContext,order.order_sn,order.total);
            }
        });
    }

    private void toCancel(Button btn_cancel, final String order_sn){
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SimpleDialog dialog = SimpleDialog.with(context);
                dialog.setQuestion(context.getString(R.string.order_cancel_order));
                dialog.listener = new SimpleDialog.OnEnsureListener() {
                    @Override
                    public void onEnsure() {
                        cancelTheOrder(order_sn);
                    }
                };
                dialog.show();
            }
        });
    }

    private void toReceived(Button btn_receive, final String order_sn){
        btn_receive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                orderReceived(order_sn);
            }
        });
    }

    /**
     * 删除订单
     */
    public void cancelTheOrder(String order_sn){
        Map<String,String> params = new HashMap<>();
        params.put(EXTRA_ORDER_SN,order_sn);
        Observable<EResponse> observable = EClient.with().api().post(EApi.USER_CANCELTHEORDER,params);
        EClient.with().observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                oofListener.onRefreshClick();
            }

            @Override
            public void failure(String msg) {
                Toast.makeText(mContext,msg,Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 确认收货
     */
    public void orderReceived(String order_sn){
        Map<String,String> params = new HashMap<>();
        params.put(EXTRA_ORDER_SN,order_sn);
        Observable<EResponse> observable = EClient.with().api().post(EApi.USER_RECEIVED,params);
        EClient.with().observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                oofListener.onRefreshClick();
            }

            @Override
            public void failure(String msg) {
                Toast.makeText(mContext,msg,Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     *
     * @param activity
     */
    public void listener(final EActivity activity){
        onItemClickListener = new EListener<Order>() {
            @Override
            public void onInvoked(View view, Order data, int position) {
                OrderDetailActivity.toDetail(activity,data.order_sn,data.status);
            }
        };
    }

    /**
     * fragment回调
     */
    public interface OnOrderFragmentListener {
        public void onRefreshClick();
    }

    /**
     * 监听
     * @param Listener
     */
    public void setOnRefreshClickListener (OnOrderFragmentListener  Listener) {
        this.oofListener = Listener;
    }
}
